<?php

namespace app\adminapi\export;

use app\services\activity\coupon\StoreCouponServices;
use Carbon\Carbon;

class StoreCouponExport extends BaseExport
{
    protected $header = [
        '优惠券名称', '有效时间', '券状态', '待领取', '已领取', '已使用', '活动开启/关闭'
    ];

    public function createData(array $where)
    {
        $list = app()->make(StoreCouponServices::class)->getList($where)['list'];

        $data = [];

        $status_map = [
            0 => '关闭',
            1 => '开启'
        ];

        $time_status_text = '';
        foreach ($list as $item) {
            $expire_time = ($item['start_time'] && $item['end_time']) ? Carbon::parse($item['start_time'])->format('Y-m-d H:i:s') . '至'
                . Carbon::parse($item['end_time'])->format('Y-m-d H:i:s') : '';

            if ($item['start_time'] > time()) {
                $time_status_text = '未开始';
            } else if ($item['start_time'] <= time() && $item['end_time'] > time()) {
                $time_status_text = '进行中';
            } else if ($item['end_time'] <= time()) {
                $time_status_text = '已结束';
            }

            $data[] = [
                $item['title'] ?? '',
                $expire_time,
                $time_status_text,
                $item['total_count'] - $item['get_count'],
                $item['get_count'],
                $item['use_count'],
                $status_map[$item['status']]
            ];
        }

        return $data;
    }

}